import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class TestDemo {
    public static void main(String[] args) {
        ArrayList<String> arrayList1 = new ArrayList<>();// 初始容量为0
        arrayList1.add("1");
        System.out.println(arrayList1);

        ArrayList<String> arrayList2 = new ArrayList<>(13);//初始容量为 13
    }

    public static void main6(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("c");
        arrayList.add("b");
        System.out.println(arrayList);
        System.out.println("=================");
        List<String> list = arrayList.subList(0,3);// 左闭右开 ["a","b") == "a","b","c"
        System.out.println(list);
        System.out.println(arrayList);
        System.out.println("===========");
        list.set(1,"g");
        System.out.println(list);
        System.out.println(arrayList);
//        int n = arrayList.lastIndexOf("f");
//        System.out.println(n);
//        boolean bl = arrayList.contains("c");
//        System.out.println(bl);
//        boolean bl2 = arrayList.contains("f");
//        System.out.println(bl2);
//        arrayList.clear();
//        System.out.println(arrayList);
//        String str = arrayList.set(3,"e");
//        System.out.println(str);
//        System.out.println(arrayList);
//        String ret = arrayList.get(2);
//        System.out.println(ret);
//        System.out.println(arrayList);
//        boolean bl = arrayList.remove("b");
//        System.out.println(bl);
//        System.out.println(arrayList);
//        String str = arrayList.remove(1);
//        System.out.println(str);// 打印删除元素
//        System.out.println(arrayList);
    }

    public static void main5(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");// 添加List元素
        System.out.println(list);
        System.out.println("===============");
        list.add(1,"g");// 在List指定位置中添加元素
        System.out.println(list);
        System.out.println("========");
        List<String> list1 = new ArrayList<>();
        list1.add("x");
        list1.add("y");
        list.addAll(list1);// 将一个list1 整体添加 list中
        System.out.println(list);
    }

    public static void main4(String[] args) {
//        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        ListIterator<String> it = list.listIterator();
        while (it.hasNext()){
            String str = it.next();
            if (str.equals("hello")){
                list.add("author");
            }else {
                System.out.print(str +" ");
            }
        }
        System.out.println();
        System.out.println(list);
    }

    public static void main3(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        ListIterator<String> it = list.listIterator();
        while (it.hasNext()){
            String str = it.next();
            if (str.equals("hello")){
                it.remove();
            }else {
                System.out.print(str +" ");
            }
        }
    }

    public static void main2(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");https://blog.csdn.net/DarkAndGrey/article/details/121657343?spm=1001.2014.3001.5501
        System.out.println(list);
        System.out.println("=======");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+ " ");
        }
        System.out.println();// 换行
        System.out.println("=======");
        for (String s:list) {
            System.out.print(s+" ");
        }
        System.out.println();
        System.out.println("=====迭代器打印========");
        Iterator<String> it =  list.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+ " ");
        }

        System.out.println();
        System.out.println("=====List迭代器打印========");
        ListIterator<String> listIt = list.listIterator();
        while (listIt.hasNext()){
            System.out.print(listIt.next() + " ");
        }
    }

    public static void main1(String[] args) {
        // List 是一个接口，虽然不能实例化，但是 List引用 能 引用 实现类的对象（实现了List接口的类）
        // <String> 指定 输入的数据类型
        List<String> list = new ArrayList<>(10);// 更通用，List接口可以引用的实现类，不止ArrayList 一个类
        // 构造一个具有10个容量的列表

        ArrayList<String> arrayList = new ArrayList<>();// ArrayList 类只能用于它本身
        // 构造一个空列表


        ArrayList<String> arrayList1 = new ArrayList<>(arrayList);

//        ArrayList<Integer> arrayList2 = new ArrayList<>(arrayList1);
    }
}
